package wili.logic;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import wili.c.Misc9a;
import wili.misc.CarryBitIterator3;

class ValueMapIterator {
	public final Map<String, List<Object>> optValuesMap = new TreeMap<>();

	final CarryBitIterator3 it;

	final List<String> keys;

	static StrToListMapFromFile VAR_TO_OPT_VALUES_MAP;

	public ValueMapIterator(Set<String> varNameSet, File file) {
		if (VAR_TO_OPT_VALUES_MAP == null) {
			VAR_TO_OPT_VALUES_MAP = new StrToListMapFromFile(file);
		}
		load_BoolExpelMap();
		Map<String, List<Object>> bigMap = VAR_TO_OPT_VALUES_MAP.getMap();
		for (String va : varNameSet) {
			String key = BoolExpelMap2.get(va);
			if (key != null) {
				optValuesMap.put(key, bigMap.get(key));
			} else {
				optValuesMap.put(va, bigMap.get(va));
			}
		}
		this.keys = new ArrayList<>(optValuesMap.keySet());
		if ("456".isEmpty()) {
			it = null;
			return;
		}
		List<List<Object>> valuesList = Misc9a.valuesOfListMap_orderByKeys(optValuesMap, keys);
		this.it = CarryBitIterator3.newInst(valuesList);
	}

	public boolean hasNext() {
		return it.hasNext();
	}

	/**
	 * 布尔排斥集
	 */
	Map<String, String[]> BoolExpelMap = new HashMap<>();

	/**
	 * 布尔排斥集，反射映射
	 */
	Map<String, String> BoolExpelMap2 = new HashMap<>();

	protected void load_BoolExpelMap() {
		String[] vv = new String[] { "addMode", "editMode", "viewMode" };
		BoolExpelMap.put("$editMode", vv);
		for (String v : vv) {
			BoolExpelMap2.put(v, "$editMode");
		}
	}

	/**
	 * 更新布尔排斥集
	 */
	private void update_BoolExpelMap(Map<String, Object> ret) {
		for (String oneName : BoolExpelMap.keySet()) {
			String[] varNames = BoolExpelMap.get(oneName);
			String curName = (String) ret.remove(oneName);
			if (curName == null)
				continue;
			for (int i = 0; i < varNames.length; i++) {
				ret.put(varNames[i], curName.equals(varNames[i]));
			}
		}
	}

	@SuppressWarnings("rawtypes")
	public Map<String, Object> next() {
		int[] inds = it.next();
		Map<String, Object> ret = new HashMap<>();
		for (int i = 0; i < inds.length; i++) {
			String key = keys.get(i);
			List lis = optValuesMap.get(key);
			ret.put(key, lis.get(inds[i]));
		}
		update_BoolExpelMap(ret);
		return ret;
	}
}